package com.haois.mp.database.module.wxserver.rest;

import com.haois.mp.database.commom.enums.ApiResultEnum;
import com.haois.mp.database.commom.constant.Reply;
import com.haois.mp.database.module.offaccounts.service.UserFollowService;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfAccountRequest;
import com.haois.mp.database.module.basic.controller.BaseController;
import com.haois.mp.database.commom.enums.MpTypeEnum;

import com.haois.mp.database.module.common.daomain.WxUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by RuiHaoZ on 2018/4/10 18:58
 */
@RestController
@RequestMapping("/api/wxmp/kefu")
public class KeFuMsgController extends BaseController {

    @Autowired
    private UserFollowService userFollowService;

    /**
     * @api {POST} /api/wxmp/kefu/send 发送客服消息
     * @apiGroup sysKeFu
     * @apiName sendKeFuMassage
     * @apiDescription 发送客服消息
     * @apiParam {String} content 客户信息内容
     * @apiParam {String} openId 加密微信号
     * @apiParam {String} memberNum 编号
     * @apiParamExample {json} 请求样例：
     * POST /api/wxmp/kefu/send
     * @apiSuccessExample {json} 返回样例:
     * {
     * <p>
     * }
     */
    @RequestMapping(value = "/send", method = RequestMethod.POST, params = {"content"})
    public String sendKeFuMassage(String content, String openId, String memberNum) {
        Reply reply = null;
        if (StringUtils.isBlank(openId)) {
            if (StringUtils.isBlank(memberNum)) {
                reply = new Reply(ApiResultEnum.API_10.getCode(), "openId和memberNum不能同时为空");
            } else {
                WxUser wxUser = userFollowService.getByUserId(memberNum, MpTypeEnum.OFFACCOUNTS.getType());
                if (wxUser != null && wxUser.getSubscribe()) {
                    openId = wxUser.getOpenId();
                } else {
                    reply = new Reply(ApiResultEnum.API_10.getCode(), "编号为【" + memberNum + "】未关注公众号");
                }
            }
        }
        if (StringUtils.isNotBlank(openId)) {
            WxMpKefuMessage message = new WxMpKefuMessage();
            message.setMsgType(WxConsts.KefuMsgType.TEXT);
            message.setToUser(openId);
            message.setContent(content);
            try {
                boolean result = wxMpService.getKefuService().sendKefuMessage(message);
                reply = new Reply(ApiResultEnum.API_00.getCode(), "发送结果请查看data值", result);
            } catch (WxErrorException e) {
                e.printStackTrace();
                reply = new Reply(ApiResultEnum.API_10.getCode(), "请求失败", e.getError());
            }
        }
        return reply.toJson();
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addKeFu(String kfAccount) {
        Reply reply = null;

        try {
            WxMpKfAccountRequest request = WxMpKfAccountRequest.builder()
                    .kfAccount(kfAccount).nickName("我晕").build();
            boolean result = wxMpService.getKefuService().kfAccountAdd(request);

            reply = new Reply(ApiResultEnum.API_00.getCode(), "发送结果请查看data值", result);
        } catch (WxErrorException e) {
            e.printStackTrace();
            reply = new Reply(ApiResultEnum.API_10.getCode(), "请求失败", e.getError());
        }
        return reply.toJson();
    }
}
